<?php
/**
 * Description: 行测-笔记-编辑
 * User: devforma
 * Date: 16/11/4
 * Time: 16:31
 */

class SetQuestionNotes implements iController {
    private static $MODULE_DIR;

    function __construct() {
        self::$MODULE_DIR  = __DIR__ . '/modules/';
    }

    public function output($queryArgs) {
        global $uddb;

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['question_id']) || empty($queryArgs['content']))
            return genOutput(1000);

        $questionNote  = getQuestionNotes($queryArgs);

        if (!empty($questionNote)) {

            $where = [
                'user_id'       => $queryArgs['user_id'],
                'question_id'   => $queryArgs['question_id']
            ];

            $status = $uddb->update("user_question_notes", ['content' => trim($queryArgs['content'])],$where);

        } else {

            $noteData = [
                'user_id'       => $queryArgs['user_id'],
                'question_id'   => $queryArgs['question_id'],
                'content'       => $queryArgs['content'],
                'updated_time'  => date('Y-m-d H:i:s'),
                'created_time'  => date('Y-m-d H:i:s')
            ];
            $status = $uddb->insert("user_question_notes",$noteData);
        }

        $result = [];
        if ($status)
            $result['content'] = nl2br($queryArgs['content']);

        return genOutput(1, $result);
    }
}